package com.sikadwafastdaisfondturbo.pretloan.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import com.sikadwafastdaisfondturbo.pretloan.R
import com.sikadwafastdaisfondturbo.pretloan.app.App
import com.sikadwafastdaisfondturbo.pretloan.databinding.DialogPersonalBinding
import com.sikadwafastdaisfondturbo.pretloan.pro.ProDialog
import com.sikadwafastdaisfondturbo.pretloan.ui.PrivacyPolicyActivity
import com.sikadwafastdaisfondturbo.pretloan.ui.user.activity.AccountListActivity
import com.sikadwafastdaisfondturbo.pretloan.ui.user.activity.FeedbackActivity
import com.sikadwafastdaisfondturbo.pretloan.utils.config.Configs
import com.sikadwafastdaisfondturbo.pretloan.utils.event.EventType
import com.sikadwafastdaisfondturbo.pretloan.utils.helper.SikadwaKefuConfigHelper
import com.sikadwafastdaisfondturbo.pretloan.utils.http.HttpHelper

/**
 * Author : wwdeng
 * Date : 2024/5/24 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 个人中心
 */
class PersonalDialog: ProDialog {
    private lateinit var binding: DialogPersonalBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context?, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun initWindow() {
        val params = window?.attributes
        params?.width = App.getPhoneWidth()
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    override fun getRootView(): View {
        binding = DialogPersonalBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    @SuppressLint("SetTextI18n")
    override fun initView() {
        binding.textMobile.text = "+"+Configs.getSPString(Configs.SPKEY.SIKADWA_TIMEZONE_CODE)+Configs.getSPString(Configs.SPKEY.SIKADWA_MOBILE)
        //获取版本名称
        val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
        binding.textVersion.text = String.format(context.getString(R.string.personal_version), packageInfo.versionName)

        //默认不显示删除数据按钮
        binding.dataClearLayout.visibility = View.GONE
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose)
        setOnClickListener(R.id.accountLayout)
        setOnClickListener(R.id.feedbackLayout)
        setOnClickListener(R.id.rateUsLayout)
        setOnClickListener(R.id.languageLayout)
        setOnClickListener(R.id.btnLoginOut)
        setOnClickListener(R.id.dataClearLayout)
        setOnClickListener(R.id.updateLayout)
        setOnClickListener(R.id.textPrivacy)
    }

    override fun initData() {
        val zoneCode = Configs.getSPString(Configs.SPKEY.SIKADWA_TIMEZONE_CODE)
        val zoneCountry = Configs.getSPString(Configs.SPKEY.SIKADWA_TIMEZONE_COUNTRY)
        //坦桑需要选择语言
        if (zoneCode == "255" || zoneCountry == "tanzania"){
            binding.languageLayout.visibility = View.VISIBLE
        } else {
            binding.languageLayout.visibility = View.GONE
        }

        //是否有版本更新
        binding.updatePointView.visibility = View.GONE
        if (SikadwaKefuConfigHelper.isSikadwaShowUpdate()){
            binding.updatePointView.visibility = View.VISIBLE
        }
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when (v?.id) {
            R.id.accountLayout -> {//账户
                val intent = Intent(context, AccountListActivity::class.java)
                context.startActivity(intent)
            }
            R.id.feedbackLayout -> {//意见反馈
                val intent = Intent(context,FeedbackActivity::class.java)
                context.startActivity(intent)
            }
            R.id.rateUsLayout -> {//打分
                mCallback?.onRateUsClick()
            }
            R.id.languageLayout -> {//切换语言
                mCallback?.onSikadwaCheckLangClick()
            }
            R.id.updateLayout -> {//版本更新
                HttpHelper.addEvent(EventType.CLICK.CLICK_UPDATE_CHECK,"Personal page")
                mCallback?.onSikadwaUpdateClick()
            }
            R.id.dataClearLayout -> {//清除数据
                mCallback?.onSikadwaDataClearClick()
            }
            R.id.btnLoginOut -> {//退出登录
                HttpHelper.addEvent(EventType.CLICK.CLICK_USER_LOGINOUT,eventRemark)
                mCallback?.onSikadwaLoginOutClick()
            }
            R.id.textPrivacy -> {//隐私政策
                val intent = Intent(context,PrivacyPolicyActivity::class.java)
                context.startActivity(intent)
            }
        }
    }

    /**
     * 设置是否显示删除数据按钮
     */
    fun showSikadwaDataClearButtonVisibility(isShow: Boolean){
        if (isShow){
            binding.dataClearLayout.visibility = View.VISIBLE
        } else {
            binding.dataClearLayout.visibility = View.GONE
        }
    }

    interface IDialogCallback {
        /**
         * 打分
         */
        fun onRateUsClick()
        /**
         * 清除用户数据
         */
        fun onSikadwaDataClearClick()
        /**
         * 退出登录
         */
        fun onSikadwaLoginOutClick()

        /**
         * 版本更新
         */
        fun onSikadwaUpdateClick()
        /**
         * 切换语言
         */
        fun onSikadwaCheckLangClick()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PERSONAL_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addEvent(EventType.DIALOG.DIALOG_PERSONAL_DISMISS,eventRemark)
    }
}